'use client';

import { useToast } from '@/hooks/use-toast';
import { result } from '@/lib/utils';
import { banUnbanUser } from '@/service/actions/user';
import ConfirmButton from '../common/confirm-button';

export default function UserBanUnban({
  button,
  userId,
  banned,
}: { button: React.ReactNode; userId: string; banned: boolean }) {
  const { toast } = useToast();
  return (
    <ConfirmButton
      title={banned ? '解除封禁' : '封禁用户'}
      description={banned ? '确认解除该用户的封禁吗？' : '确认封禁该用户吗？'}
      button={button}
      onClick={async () => {
        const { error } = banned
          ? await banUnbanUser({ userId, banned: false })
          : await banUnbanUser({ userId, banned: true });
        error
          ? toast({ title: error.message, variant: 'destructive' })
          : toast({ title: '操作成功' });
        return error ? result(error, error?.message ?? '操作失败') : result();
      }}
    />
  );
}
